<%--
  Created by IntelliJ IDEA.
  User: lichengming
  Date: 2017/6/15
  Time: 下午12:11
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/resources.jsp" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>用户管理</title>

    <link rel="stylesheet" href="/starCms/css/upload/webuploader.css">
    <link rel="stylesheet" href="/starCms/css/tree/bootstrap-treeview.min.css">
    <script src="/starCms/js/jquery.md5.js"></script>
    <script src="/starCms/js/tree/bootstrap-treeview.min.js"></script>

    <style type="text/css">

        .ems_add_gbtn {
            flex: auto;
            display: flex;
            justify-content: flex-end;
        }

        .ems_add_heading {
            display: flex;
            flex-direction: row;
            align-items: center;
            padding: 5px 10px;
        }
        .ems_form_reset {
            margin-left: 10px;
        }

        .ems_form_checkbox_g>label {
               width: 120px;
               margin-right: 10px;
        }

        .ems_form_checkbox_g>label>a {
            display: none;
            text-decoration: none;
        }

        #add_new_role>a {
            text-decoration: none;
        }

        .well_close {
            position: absolute;
            top: 10px;
            right: 10px;
        }

        #paramsList .well {
            position: relative;
        }

        #returnParamList .well {
            position: relative;
        }
    </style>
</head>
<body>
<div class="ems_form">
    <div class="panel panel-default">
        <div class="panel-heading ems_add_heading">
            <h3 class="panel-title" style="color: inherit;">服务器参数添加新增</h3>

            <div class="ems_add_gbtn">
                <button type="button" class="btn btn-primary ems_form_submit">
                    <span class="glyphicon glyphicon-check"></span> 保存
                </button>
                <button type="button" class="btn btn-default ems_form_reset">
                    <span class="glyphicon glyphicon-shopping-cart"></span> 重置
                </button>
            </div>
        </div>

        <form class="form-horizontal ems_internal_form">
            <div class="form-group">
                <label class="col-sm-3 control-label">服务器名称：</label>
                <div class="col-sm-6">
                    <input type="text" name="serverName" required value="${interfaceRecord.name}" id="serverName" class="form-control" placeholder="必填，长度不超过32">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">环境：</label>
                <div class="col-sm-6">
                    <input type="text" required name="env" id="env" value="${interfaceRecord.url}" class="form-control" placeholder="必填，长度不超过64">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">端口：</label>
                <div class="col-sm-6">
                    <input type="number" name="port" required value="${interfaceRecord.context_path}" id="port" class="form-control" placeholder="必填,长度不超过32">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-3 control-label">IP：</label>
                <div class="col-sm-6">
                    <input type="text" required name="ip" id="ip" value="${interfaceRecord.url}" class="form-control" placeholder="必填，长度不超过64">
                </div>
            </div>
        </form>
    </div>
</div>

<div class="ems_table_content">
    <div class="panel panel-default">
        <div class="panel-heading">服务器列表</div>
        <div id="table_tools">
            <div class="form-inline" role="server_form">
                <button type="button" id="server_edit" class="btn btn-outline btn-primary">
                    <i class="glyphicon glyphicon-edit" aria-hidden="true"></i>
                    编辑
                </button>
                <button type="button" id="server_delete" class="btn btn-outline btn-danger">
                    <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
                    删除
                </button>
                <button type="button" id="server_stop" class="btn btn-warning">
                    <i class="glyphicon glyphicon-remove" aria-hidden="true"></i>
                    停用
                </button>
                <button type="button" id="server_start" class="btn">
                    <i class="glyphicon glyphicon-ok" aria-hidden="true"></i>
                    启动
                </button>
            </div>
        </div>
        <table id="merchantTable"
               data-url="/starCms/serverConfig/pagination">
            <thead>
            <tr>
                <th data-field="state" data-checkbox="true"></th>
                <th data-field="server_name" >服务器名称</th>
                <th data-field="env">环境</th>
                <th data-field="ip">IP地址</th>
                <th data-field="port">端口</th>
                <th data-field="status" data-formatter="statusFormat">状态</th>
            </tr>
            </thead>
        </table>
    </div>
</div>
<script>
    function statusFormat(srcValue) {
        if (srcValue == 1) {
            return "<span style='color: #3cb371;font-weight: bold' >正常</span>";
        }else {
            return "<span style='color: red;font-weight: bold'>停用</span>";
        }
    }
</script>

<script>

    $(function () {

        $("#merchantTable").createTable();

        //窗体
        var dialogOption = {
            width:"500px",
            height:"450px",
            url:"/starCms/serverConfig/edit",
            yesFn:function(){
                var result = serverSave();
                if (result) search('#merchantTable',getQueryParams());
                return result;
            }
        }
        //编辑
        $("#server_edit").on("click",function () {
            var selectRows =  $('#merchantTable').bootstrapTable('getSelections');
            if (selectRows.length == 0) {
                $.errorAlert("请选择要编辑的数据");
                return;
            }
            if (selectRows.length > 1) {
                $.errorAlert("暂时不支持多行数据编辑");
                return;
            }

            dialogOption.title = "服务器编辑";
            dialogOption.data = {id : selectRows[0].id};
            $("#server_edit_dialog").openDialog(dialogOption);
        });
    })
</script>

<script>

    //删除
    $("#server_delete").on("click",function () {
        var selectRows =  $('#merchantTable').bootstrapTable('getSelections');
        if (selectRows.length == 0) {
            $.errorAlert("请选择要删除的数据");
            return;
        }
        if(selectRows.length >= 2){
            $.errorAlert("不支持多行删除");
            return;
        }

        for (var i = 0;i<selectRows.length;i++) {
            var server_id = selectRows[i].id;
        }
        $.emsAjax({
            data:{server_id:server_id},
            type:"POST",
            url:"/starCms/serverConfig/delete",
            success:function(resp) {
                if (resp.status == "000") {
                    $.alert(resp.message,null,null,function () {
                        window.location.href = "/starCms/serverConfig/index"
                    })
                }else {
                    $.errorAlert("删除失败");
                }
            }
        });
    });
</script>

<script>

    //废止
    $("#server_stop").on("click",function () {
        var selectRows =  $('#merchantTable').bootstrapTable('getSelections');
        if (selectRows.length == 0) {
            $.errorAlert("请选择要停止的数据");
            return;
        }
        if(selectRows.length >= 2){
            $.errorAlert("不支持多行停止");
            return;
        }

        for (var i = 0;i<selectRows.length;i++) {
            var server_id = selectRows[i].id;
        }
        $.emsAjax({
            data:{server_id:server_id},
            type:"POST",
            url:"/starCms/serverConfig/serverStop",
            success:function(resp) {
                if (resp.status == "000") {
                    $.alert(resp.message,null,null,function () {
                        window.location.href = "/starCms/serverConfig/index"
                    })
                }else {
                    $.errorAlert("停止失败");
                }
            }
        });
    });
</script>

<script>

    //启动
    $("#server_start").on("click",function () {
        var selectRows =  $('#merchantTable').bootstrapTable('getSelections');
        if (selectRows.length == 0) {
            $.errorAlert("请选择要启动的数据");
            return;
        }
        if(selectRows.length >= 2){
            $.errorAlert("不支持多行启动");
            return;
        }

        for (var i = 0;i<selectRows.length;i++) {
            var server_id = selectRows[i].id;
        }
        $.emsAjax({
            data:{server_id:server_id},
            type:"POST",
            url:"/starCms/serverConfig/serverStart",
            success:function(resp) {
                if (resp.status == "000") {
                    $.alert(resp.message,null,null,function () {
                        window.location.href = "/starCms/serverConfig/index"
                    })
                }else {
                    $.errorAlert("启动失败");
                }
            }
        });
    });
</script>


<script>

    $.validator.setDefaults({
        //认证成功
        submitHandler: function(form) {
            var data = {};
            data["serverName"] = $.trim($("#serverName").val());
            data["port"] = $.trim($("#port").val());
            data["env"] = $.trim($("#env").val());
            data["ip"] = $.trim($("#ip").val());
            saveServer(data);
        }
    });

    function saveServer(data) {
        $.emsAjax({
            url:"/starCms/serverConfig/save",
            type:"POST",
            data:data,
            success:function (resp) {
                if (resp.status == "000") {
                    $.alert(resp.message,null,null,function () {
                        window.location.href = "/starCms/serverConfig/index"
                    })
                }else {
                    $.errorAlert("保存失败");
                }
            }
        })
    }

    $(function () {

        $("form").each(function (index,val) {
            $(val)[0].reset();
        });

        //表单验证
        $(".ems_internal_form").validate();

        $(".ems_form_submit").on("click",function () {
            var form = $(this).parents(".ems_form").find("form:first");
            form.submit();
        });

        $(".ems_form_reset").on("click",function () {
            $(this).parents(".ems_form").find("form:first")[0].reset();
        })
    })
</script>

<div id="server_edit_dialog"></div>
</body>
</html>
